{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Grove Temperature Sensor example\n",
    "\n",
    "----\n",
    "* [Introduction](#Introduction)\n",
    "* [Setup the board](#Setup-the-board)\n",
    "* [Setup the sensor](#Setup-the-sensor)\n",
    "* [Read from the sensor](#Read-from-the-sensor)\n",
    "* [Display a graph](#Display-a-graph)\n",
    "\n",
    "----\n",
    "## Introduction\n",
    "\n",
    "\n",
    "The PYNQ-Z1 and PYNQ-Z2 boards have two Pmod ports and an Arduino interface. The PYNQ-Z2 also has a Raspberry Pi interface. A number of Pmod, Grove, and  Peripherals are supported by PYNQ. \n",
    "\n",
    "Pmods can be plugged directly into the Pmod port. Grove Peripherals can be connected to the Pmod Port or Arduino header through adapter boards.\n",
    "\n",
    "The external pins of these interfaces are connected to PL pins. This means the logic to control an external peripheral must be implemented in the PL in an Overlay. Pmods, Grove and Arduino peripherals can be used with IOPs in the *base* Overlay for the PYNQ-Z1 and PYNQ-Z2. \n",
    "\n",
    "This notebook will show how to use the [Grove Temperature Sensor v1.2](http://www.seeedstudio.com/wiki/Grove_-_Temperature_Sensor_V1.2) with the Grove ADC [Grove Temperature Sensor v1.2](http://wiki.seeedstudio.com/Grove-I2C_ADC/) on the PYNQ-Z1 or PYNQ-Z2 board. The Grove Temperature sensor produces an analog signal, and requires an ADC. You will also see how to plot a graph using _matplotlib_, a Python package for 2D plots. \n",
    "\n",
    "A Grove Temperature sensor, a Grove ADC, and a Pynq Grove Adapter Adapter are required for this notebook example (a Pynq Arduino adapter could also be used instead of the Pynq Grove Adapter).\n",
    "\n",
    "The driver for the Temperature sensor running on the IOP supports reading a single value of temperature, or reading and logging of multiple values at regular intervals."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----\n",
    "## Setup the board\n",
    "\n",
    "Start by loading the Base Overlay."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pynq.overlays.base import BaseOverlay\n",
    "base = BaseOverlay(\"base.bit\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup the sensor\n",
    "1. Connect the ***pmod2grove*** to ***PMODB***. \n",
    "2. Connect ***Grove ADC*** port ***J1*** (SCL, SDA, VCC, GND) to port ***G4*** of the Pynq Grove Adapter. \n",
    "3. Connect the ***Grove TMP*** to port ***J2*** of the ***Grove ADC*** (GND, VCC, NC, SIG)\n",
    "\n",
    "\n",
    "### Create an instance of the sensor\n",
    "The sensor is connected to the ADC. You will create an instance of the temperature sensor. \n",
    "\n",
    "The Grove ADC is connected to the board through the Pynq Grove adapter. This can be connected to either of the Pmod ports. The Grove ADC is an I2C peripheral. I2C requires pull-up pins on the FPGA. In the base overlay, these pins are only available on ports G3 or G4 of the Pynq Grove adapter, so the ADC must be connected to one of these ports. The Pmod port (PMODA, or PMODB), and the pins on the adapter are specified when the instance is created."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "from pynq.lib.pmod import Grove_TMP\n",
    "from pynq.lib.pmod import PMOD_GROVE_G4 # import constants\n",
    "\n",
    "# Grove2pmod is connected to PMODB (2)\n",
    "# Grove ADC is connected to G4 (pins [6,2])\n",
    "tmp = Grove_TMP(base.PMODB, PMOD_GROVE_G4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read from the sensor\n",
    "Internally, the Grove ADC provides a raw sample which is the resistance of the sensor. In the IOP, this value is converted into a temperature value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "temperature = tmp.read()\n",
    "print(float(\"{0:.2f}\".format(temperature)),'degree Celsius')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can run the cell above a number of times."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Start logging once every 100ms for 10 seconds\n",
    "Executing the next cell will start logging the temperature sensor values every 100ms, and will run for 10s. You can try touch/hold the temperature sensor to vary the measured temperature.\n",
    "\n",
    "You can vary the logging interval and the duration by changing the values in the cell below. The raw samples are stored in the internal memory, and converted into temperature values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "ms_delay = 100\n",
    "delay_s = 10\n",
    "tmp.set_log_interval_ms(ms_delay)\n",
    "tmp.start_log()\n",
    "time.sleep(delay_s) # Change input during this time\n",
    "tmp_log = tmp.get_log()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----\n",
    "## Display a graph\n",
    "\n",
    "Use matplotlib to display a graph of the temperature sensor data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "plt.plot(range(len(tmp_log)), tmp_log, 'ro')\n",
    "plt.title('Grove Temperature Plot')\n",
    "min_tmp_log = min(tmp_log)\n",
    "max_tmp_log = max(tmp_log)\n",
    "plt.axis([0, len(tmp_log), min_tmp_log, max_tmp_log])\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
